
public class Test {
  private static final int NUM_THREADS = 1;
  private static final int NUM_ITERS = 10000;
  private static final int QUEUE_TYPE = 0;
  private static final int TEST_TYPE = 0;

  public static int numThreads = NUM_THREADS; 
  public static int numIters = NUM_ITERS;
  private static int queueType = QUEUE_TYPE;
  private static int testType = TEST_TYPE;

  public static void main(String[] args) throws InterruptedException {		
    setParams(args);

    Queue queue = queueType == 0 ? new LockQueue() : 
    			  queueType == 1 ? new LFQueue() : 
                  queueType == 2 ? new WFQueue() : 
                                   new OptWFQueue();
                                   
	String queueTypeString =  	queueType == 0 ? "Lock" : 
								queueType == 1 ? "LF" :
                  				queueType == 2 ? "WF" : 
                                   		 		 "OptWF";                                   

    //		System.out.println("Starting test " + testType " with queue " + queueType + ", " + numThreads + " threads, " + numIters + " iters");

    TestThread[] threads = new TestThread[numThreads];

    for (int i = 0; i < threads.length; i++) {
      threads[i] = new TestThread(queue, i, testType, numIters);         
    }

    for (int i = 0; i < threads.length; i++) {
      threads[i].start();
	}
         
    long estimatedTime = 0;
    for (int i = 0; i < threads.length; i++) {
      threads[i].join();
      estimatedTime = Math.max(estimatedTime, threads[i].estimatedTime);
    }


    //DecimalFormat formatter = new DecimalFormat("####.##");
    // System.out.println("Done after " + formatter.format(estimatedTime / 1000) + " usec");		

    System.out.print("queueType= "+queueTypeString + ", numThreads=" + numThreads + ", numIters=" + 
                     numIters /*+ ", testType=" + testType */+ ", estimatedTime=" + estimatedTime/1000000000.0);

    System.out.println();		
  }

  protected static void printHelp() {
    System.out.println("Run command: java -jar queue.jar [thr] [iters] [queue] [test]");
    System.out.println("  [thr]   is the number of threads, default: " + NUM_THREADS);
    System.out.println("  [iters] is the number of iterations, default: " + NUM_ITERS);
    System.out.println("  [queue] is the type of queue: 0 - Lock, 1 - LF, 2 - WF, 3 - Opt WF, default: " + QUEUE_TYPE);
    System.out.println("  [test]  is the type of test: 0 - enq-deq pairs on empty queue, 1 - 50% enqs on queue with 1000 elements, default: " + TEST_TYPE);
  }

  protected static void setParams(String args[]) {	
    if (args.length > 0) {
      if (args[0].equals("help")) {			
        printHelp();
        System.exit(0);
      }
      numThreads = new Integer(args[0]); 
    }

    if (args.length > 1) {
      numIters = new Integer(args[1]); 
    }

    if (args.length > 2) {
      queueType = new Integer(args[2]);
      if (queueType < 0 || queueType > 2) {
        printHelp();
        System.exit(-1);
      }
    }

    if (args.length > 3) {
      testType = new Integer(args[3]);
      if (testType < 0 || testType > 5) {
        printHelp();
        System.exit(-1);
      }
    }
  }
}
